package arithmetic.LeetCode.First_Missing_Positive;

/**
 * @author zhangyanqi
 * @since 1.0 2017/10/11
 */
public class Solution {

    public int firstMissingPositive(int[] nums) {
        int result = 1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= 0 || nums[i] > nums.length) {
                continue;
            }
            if (nums[i] != i + 1) {
                int num1 = nums[i];
                int newPositon = nums[i] - 1;
                int num2 = nums[newPositon];
                nums[i] = num2;
                nums[newPositon] = num1;
                if (num1 != num2) {
                    i--;
                }
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                result = i + 1;
                break;
            } else {
                if (i == nums.length - 1) {
                    return i + 2;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] o = {2, 1};
        int i = solution.firstMissingPositive(o);
        System.out.println(i);
    }
}
